package amst.system;

import java.util.Date;
import java.util.Random;
import eduni.distributions.Pareto;

public class PacketGenerator
{

    protected int bandwidth = 100000;
	private Pareto pareto = null;
    private Random random = null;
	private String name;
	private SystemQueue queue;
	private double time = 0;
	private double simulationTime;
	private int interarrival;
    
	public PacketGenerator(String name, SystemQueue squeue, double simulationTime)
    {
		this.name = name;
		this.queue = squeue;
        this.bandwidth = 100000;
        pareto = new Pareto(1, 1.9);
		pareto.setSeed(new Date().getTime());
		random = new Random();
		random.setSeed(new Date().getTime());
		this.simulationTime = simulationTime;
    }
	
    public PacketGenerator(String name, SystemQueue queue, double simulationTime, int bandwidth_val)
    {
		this.name = name;
		this.queue = queue;
		this.simulationTime = simulationTime;
        this.bandwidth = bandwidth_val;
        this.time = 0;
        pareto = new Pareto(1, 1.9);
		pareto.setSeed(new Date().getTime());
		random = new Random();
		random.setSeed(new Date().getTime());
	}
	public double getGeneratorTime()
    {
        return this.time;
    }
    
	public void setInterarrival(int interarrival) {
		this.interarrival = interarrival;
	}

	public void generate()
    {

        double packetServiceTime = 0;
        int packet_length = 0;
        
        time = this.queue.getPacketSystem().getPSTime();        
		while(time < simulationTime)
        {
//            interarrival = (int) (25 * Math.abs(pareto.sample()));
            time += interarrival;
            
            this.queue.getPacketSystem().setPSTime(Math.max(time, this.queue.getPacketSystem().getPSTime()));
//            packet_length = (int) (200 * Math.abs(pareto.sample())); 
            packet_length = 800;
            packetServiceTime = packet_length/5;
            this.queue.addPacket(new Packet(packetServiceTime, time));            
//            System.out.println(name + " time: " + time + " systime: " +
//                    this.queue.getPacketSystem().getPSTime());
            System.out.println("Time: " + time + 
                              " Interarrival: " + interarrival + 
                              " Generuje pakiet...(" + name + "): " + packetServiceTime + 
                              " length: " + packet_length);


			try {Thread.sleep((int)interarrival);}
            catch (InterruptedException e)
                  { e.printStackTrace(); }
		}
	}
}
